package com.kyzh.core.adapters.decoration

import android.content.res.Resources
import android.graphics.Rect
import android.util.Log
import android.view.View
import androidx.recyclerview.widget.RecyclerView

/**
 *
 * @Author:         yu
 * @CreateDate:     2021/7/14 11:53
 */
class SpacesItemDecoration(var space :Float) :RecyclerView.ItemDecoration() {
    override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
        outRect.left = space.toInt();
        outRect.right = space.toInt();
        outRect.bottom = space.toInt();

        // Add top margin only for the first item to avoid double space between items
        if (parent.getChildLayoutPosition(view) != 0)
            outRect.top = px2dp(space);

    }
    fun px2dp(dpValue: Float): Int {
        val fl = 0.5f + dpValue * Resources.getSystem().getDisplayMetrics().density
        Log.e("TAG", "px2dp: $fl", )
        return fl.toInt()
    }

}